﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CosyTrade.Organization;
using CosyTrade.UI;
using DevExpress.XtraEditors.Controls;

namespace CosyTrade.ClientForm.Trade
{
    public partial class ComfirmReciveExportBill : FormBase
    {
        private readonly Guid reciveCompanyKey;
        private readonly DateTime minDate;
        private DateTime reciveDate;
        private Employee reciveEmployee;

        public ComfirmReciveExportBill(Guid reciveCompanyKey, DateTime minDate)
        {
            this.reciveCompanyKey = reciveCompanyKey;
            this.minDate = minDate;
            InitializeComponent();
        }

        private void ComfirmReciveExportBill_Load(object sender, EventArgs e)
        {
            dtReciveDate.EditValue = DateTime.Now;
            cbEmployee.Properties.Items.Clear();
            IList<Employee> emps = Operation.OrganizationSystem.GetEmployeeList(reciveCompanyKey);
            foreach (Employee emp in emps)
            {
                ImageComboBoxItem item = new ImageComboBoxItem(emp.EmpName, emp);
                cbEmployee.Properties.Items.Add(item);
                if (emp.PrimaryKey == GlobalInfo.CurrentEmployee.PrimaryKey) cbEmployee.SelectedItem = item;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            reciveEmployee = cbEmployee.EditValue as Employee;
            reciveDate = (DateTime) dtReciveDate.EditValue;
            
            if(reciveEmployee == null)
            {
                Functions.ShowMessageBox("请选择负责签收的员工");
                return;
            }
            if(reciveDate < minDate)
            {
                Functions.ShowMessageBox("签收日期不能小于发货日期");
                return;
            }

            DialogResult = DialogResult.OK;
        }

        public DateTime ReciveDate
        {
            get { return reciveDate; }
        }

        public Employee ReciveEmployee
        {
            get { return reciveEmployee; }
        }
    }
}
